package org.insia.smartposse.dao;

import java.util.ArrayList;
import java.util.List;

import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.IFreelancePortfolioDAO;
import org.insia.smartposse.metier.FreelancePortfolio;
import org.insia.smartposse.metier.webservice.LightFreelancePortfolio;

/**
 * DAO for FreelancePortfolio class based on GenericDAO.
 * @author sok hout
 *
 */
public class FreelancePortfolioDAO 
extends GenericDAO<FreelancePortfolio, Long> 
implements IFreelancePortfolioDAO {
	/**
	 * Empty constructor.
	 *
	 */
	public FreelancePortfolioDAO() {
		type = FreelancePortfolio.class;
	}

	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelancePortfolioDAO#createLightFreelancePortfolio(org.insia.smartposse.metier.webservice.LightFreelancePortfolio)
	 */
	public LightFreelancePortfolio createLightFreelancePortfolio(LightFreelancePortfolio lfport) {
		try {
			FreelancePortfolio fport = new FreelancePortfolio();
			fport.setDescription(lfport.getDescription());
			fport.setFreelance(DAOFactory.getFreelanceDAO().get(lfport.getFreelance()));
			fport.setUrl(fport.getUrl());
			lfport.setId(DAOFactory.getFreelancePortfolioDAO().create(fport));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lfport;
	}
	
	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelancePortfolioDAO#createLightFreelancePortfolio(java.util.List)
	 */
	public List<LightFreelancePortfolio> createLightFreelancePortfolio(List<LightFreelancePortfolio> llfport) {
		List<LightFreelancePortfolio> LlfPort = new ArrayList<LightFreelancePortfolio>();
		try {
			for (LightFreelancePortfolio lport : llfport) {
				lport = createLightFreelancePortfolio(lport);
				LlfPort.add(lport);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LlfPort;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelancePortfolioDAO#updateLightFreelancePortfolio(org.insia.smartposse.metier.webservice.LightFreelancePortfolio)
	 */
	public LightFreelancePortfolio updateLightFreelancePortfolio(LightFreelancePortfolio lfport) {
		try {
			FreelancePortfolio fport = DAOFactory.getFreelancePortfolioDAO().get(lfport.getId());;
			fport.setDescription(lfport.getDescription());
			fport.setFreelance(DAOFactory.getFreelanceDAO().get(lfport.getFreelance()));
			fport.setUrl(fport.getUrl());
			DAOFactory.getFreelancePortfolioDAO().update(fport);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lfport;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IFreelancePortfolioDAO#updateLightFreelancePortfolio(java.util.List)
	 */
	public List<LightFreelancePortfolio> updateLightFreelancePortfolio(List<LightFreelancePortfolio> llfport) {
		List<LightFreelancePortfolio> LlfPort = new ArrayList<LightFreelancePortfolio>();
		try {
			for (LightFreelancePortfolio lport : llfport) {
				lport = updateLightFreelancePortfolio(lport);
				LlfPort.add(lport);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return LlfPort;
	}
}
